library(lme4);library(AER);library(plm);library(devtools);library(stargazer)


#install_github("https://github.com/cran/ivpack")

library(ivpack)

setwd("~/Dropbox/Florence_Paper/replication/data")


exchdat<-read.csv(file="exhange_italy_data.csv"  )



exchdat$cyear<-paste(exchdat$City,exchdat$Year,sep="-")



exchdat$myear<-paste(exchdat$Month,exchdat$Year,sep="-")

exchdat$date<-paste(exchdat$Month,exchdat$Day,exchdat$Year,sep="-")


exchdat$mdyear<-paste(exchdat$myear,exchdat$Day,sep="-")


#exchdat$city<-relevel(exchdat$City,ref="Florence")









###################

Pisa_parl<-read.csv(file="Pisa.csv")


Pisa_parl$City<-"Pisa"



Florence_parl<-read.csv(file="Florence.csv")


Florence_parl$City<-"Florence"


Papal_States_parl<-read.csv(file="Papal States.csv")

Papal_States_parl$City<-"Rome"

Genoa_parl<-read.csv(file="Genoa.csv")

Genoa_parl$City<-"Genoa"

Milan_parl<-read.csv(file="Milan.csv")

Milan_parl$City<-"Milan"



Modena_parl<-read.csv(file="Modena.csv")

Modena_parl$City<-"Bologna"

Lucca_parl<-read.csv(file="Lucca.csv")

Lucca_parl$City<-"Lucca"



city_data<-list(Pisa_parl,Florence_parl,Papal_States_parl,Genoa_parl,Milan_parl,Lucca_parl,Modena_parl)


cov<-c("City","Parliament.Presence","Year","Composition","Powers")


city_data2<-list()


for(i in 1:length(city_data)){
	
	
	city_data2[[i]]<-city_data[[i]][,cov]
	
	
	
	
}





city_data2<-do.call(rbind,city_data2)




avg<-tapply(1-exchdat$Par/100,exchdat$cyear,mean)



sd<-tapply(exchdat$Par/100+1,exchdat$cyear,sd,na.rm=T)




avg_dat<-data.frame(avg,sd,do.call(rbind,strsplit(names(avg),"-"))[,1],do.call(rbind,strsplit(names(avg),"-"))[,2]) 

names(avg_dat)<-c("avg","sd","city","year")

#avg_dat$city<-relevel(avg_dat$city,ref="Florence")



avct<-merge(avg_dat,city_data2,by.y=c("City","Year"), by.x=c("city","year"))





Urbs<-data.frame(c(45,23,33,55,90,100,23),c(44.494887,43.722839,41.902783,43.769560,45.465422,44.405650,43.837621),c(11.34262,10.401689,12.496366,11.255814,9.185924,8.946256,10.495061), c(130.23,   244.12, 394.11 , 204.11, 244.39, 289.46 ,229.43),c(1,0,1,1,1,0,0), c("Bologna","Pisa","Rome","Florence","Milan","Genoa","Lucca"))

names(Urbs)<-c("Urban_Pop","Lat","Long","Distance","Guelph","city")


avct<-merge(avct,Urbs,by="city")



avct$Silver<-ifelse(avct$city %in% c("Genoa","Milan"),1,0)



ft1<-(lm(log(avg)~Parliament.Presence,data=avct))
fit1<-robust.se(ft1)


ft2<-(lm(log(avg)~Parliament.Presence+as.factor(year),data=avct))


fit2<-robust.se(ft2)



ft3<-(lm(log(avg)~Parliament.Presence+log(Urban_Pop)+Silver+log(Distance)+as.factor(year),data=avct))

fit3<-robust.se(ft3)



##################################################################

exty<-merge(exchdat,avct,by.x=c("City","Year"),by.y=c("city","year"))

exty$Silver<-ifelse(exty$City %in% c("Genoa","Milan"),1,0)


ft5<-(lm(log(I(1-Par/100))~Parliament.Presence,data=exty))

fit5<-robust.se(ft5)


ft6<-(lm(log(I(1-Par/100))~Parliament.Presence+as.factor(Year)+as.factor(Month)+as.factor(Day),data=exty))

fit6<-robust.se(ft6)


exty$mdyear<-paste(exty$Month,exty$Day,exty$Year,sep="-")


ft7<-(lm(log(I(1-Par/100))~Parliament.Presence+as.factor(mdyear),data=exty))


fit7<-robust.se(ft7)


ft8<-(lm(log(I(1-Par/100))~Parliament.Presence+log(Urban_Pop)+Silver+log(Distance)+as.factor(Year)+as.factor(Month)+as.factor(Day),data=exty))

fit8<-robust.se(ft8)



ft9<-(lm(log(I(1-Par/100))~Parliament.Presence+log(Urban_Pop)+Silver+log(Distance)+as.factor(mdyear),data=exty))

fit9<-robust.se(ft9)




stargazer(list(fit2,fit3,fit6,fit7,fit8,fit9),digits=3,keep=c("Parliament.Presence","Urban_Pop", "Silver", "Distance","(Intercept)" ))










